/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.TikiiTorch.Deathwatch.AmmoCounter.R;
import com.TikiiTorch.Deathwatch.AmmoCounter.SpaceMarine;
import com.TikiiTorch.Deathwatch.AmmoCounter.file.FileManager;

public class MainMenu extends Activity implements OnItemClickListener,
		OnItemLongClickListener {

	String[] charList = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_menu);

		Button addChar = (Button) findViewById(R.id.btnAddChar);
		addChar.setOnClickListener(new AddCharListener());
	}

	@Override
	protected void onResume() {

		super.onResume();

		if (FileManager.charXMLExists()) {
			loadCharList();
		} else {
			FileManager.createCharXMLDir();
		}
	}

	private void loadCharList() {
		charList = FileManager.getCharList();

		ListView charListView = (ListView) findViewById(R.id.characterList);
		charListView.setOnItemClickListener(this);
		charListView.setOnItemLongClickListener(this);
		charListView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, charList));
	}

	private void startAmmoCounter(String name) {
		Intent intent = new Intent(this, DWAmmo.class);
		intent.putExtra("character", name);
		startActivity(intent);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		startAmmoCounter(charList[position]);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {

		final int index = position;

		final Context context = view.getContext();

		AlertDialog.Builder removeCharAlert = new AlertDialog.Builder(this);

		removeCharAlert.setTitle(charList[position]);

		removeCharAlert.setMessage("Would you like to delete this character?");

		removeCharAlert.setPositiveButton("Delete",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						FileManager.deleteCharFile(charList[index]);
						Toast.makeText(context, "Character Deleted",
								Toast.LENGTH_SHORT).show();
						loadCharList();
					}
				});

		removeCharAlert.setNegativeButton("Cancel", null);

		removeCharAlert.show();

		return false;
	}

	private class AddCharListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());

			alert.setTitle("Character's Name");
			final EditText input = new EditText(v.getContext());
			alert.setView(input);

			alert.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							if (input.length() > 0) {
								SpaceMarine marine = new SpaceMarine();
								marine.setName(input.getText().toString());
								FileManager.saveSpaceMarineFile(
										getApplicationContext(), marine);
								startAmmoCounter(marine.getName());
							}
						}
					});

			alert.show();

		}

	}

}
